//
//  RecommendViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RecommendViewController.h"
#import "AboutDatatype.h"
#import "MainCViewCell_1.h"
#import "MainCViewCell.h"
#import "HFCollectionRView.h"

@interface RecommendViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UICollectionView *typeView2;
@property (nonatomic, retain) UIScrollView *scrollView;

@end

// 设置标识
static NSString *indentfy = @"indentify";
static NSString *indentfy1 = @"indentify1";

@implementation RecommendViewController

- (void)dealloc
{
    [_arr release];
    [_typeView2 release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 最上面不动的滚动视图
    self.tabBarController.tabBar.translucent = NO;
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 180)];
    self.scrollView.backgroundColor = [UIColor redColor];
    self.scrollView.contentSize = CGSizeMake(375 * 5, 0);
    // 按页翻
    self.scrollView.pagingEnabled = YES;
    // 指定代理人
    _scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    [_scrollView release];

    
//    // 向scrollView上添加imageView
//    for (NSInteger i = 0; i < 5; i++) {
//        
//        // 根据
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(305 * i, 0, 305, 500)];
//        
//        // 根据循环创建图片
//        NSString *name = [NSString stringWithFormat:@"%ld.jpg", i];
//        imageView.image = [UIImage imageNamed:name];
//        
//        [self.scrollView addSubview:imageView];
//        [imageView release];

    
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 最小行间距
    flowLayout.minimumLineSpacing = 15;
    // 最小列间距
    flowLayout.minimumInteritemSpacing = 0;
    //设置，上／左／下／右边距 空间间隔数是多少
    flowLayout.sectionInset =UIEdgeInsetsMake(0, 25, 0, 25);
    //如果有多个区 就可以拉动
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //设置头部并给定大小
    [flowLayout setHeaderReferenceSize:CGSizeMake(_typeView2.frame.size.width,50)];
    _typeView2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 180, self.view.frame.size.width, self.view.frame.size.height - 180 - 49 - 64) collectionViewLayout:flowLayout];
    _typeView2.dataSource = self;
    _typeView2.delegate = self;
    _typeView2.backgroundColor = [UIColor yellowColor];
    // 注册单元格
    [_typeView2 registerClass:[MainCViewCell class] forCellWithReuseIdentifier:indentfy];
    [_typeView2 registerClass:[MainCViewCell_1 class] forCellWithReuseIdentifier:indentfy1];
    // 注册头部视图
    [_typeView2 registerClass:[HFCollectionRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [self.view addSubview:_typeView2];
        [flowLayout release];
    
    
    
    
    {// 数据
    NSString *URLstr = @"http://api.breadtrip.com/featured/";
    
    URLstr = [URLstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLstr]];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSString *requestTmp = [NSString stringWithString:operation.responseString];
        
        NSData *data = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray *tempArr = [resultDic objectForKey:@"elements"];
        
        
        self.arr = [NSMutableArray array];
        
        for (NSMutableDictionary *dic in tempArr) {
            AboutDatatype *elementsDic = [[AboutDatatype alloc] initWithDictionary:dic];
            [self.arr addObject:elementsDic];
            [elementsDic release];
            
        }
        // 刷新数据
        [_typeView2 reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
    [operation start];
    }
    
}


// section高度自定义
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.bounds.size.width, 45);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(335, 180);
    }else if (indexPath.section == 1){
        return CGSizeMake(155, 155);}
    return CGSizeMake(325, 180);
}

// 有多少个Section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

// 每个Section有多少个元素
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0 ) {
        return 5;
    }
    return 4;
}

// 每个单元格的数据
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //初始化每个单元格
        MainCViewCell_1 *cell = (MainCViewCell_1 *)[collectionView dequeueReusableCellWithReuseIdentifier:indentfy1 forIndexPath:indexPath];
        return cell;
    }else {
        MainCViewCell *cell = (MainCViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentfy forIndexPath:indexPath];
        return cell;

    }

}

//设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    
    
    if (kind == UICollectionElementKindSectionHeader) {
        //定制头部视图的内容
        HFCollectionRView *headerV = (HFCollectionRView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"forIndexPath:indexPath];
        headerV.titleLab.text =@"头部视图";
        reusableView = headerV;
    }
    return reusableView;
}


//点击单元格
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.section);
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
